package com.tianjiancloud.exception.utils;

import com.tianjiancloud.exception.CommonException;
import com.tianjiancloud.exception.ExceptionCode;

import java.util.Collection;
import java.util.Objects;

/**
 * Created by Yan on 2015/2/1.
 */
public abstract class ExceptionUtils {

    private ExceptionUtils() {

    }

    public static final void isTrue(boolean express, ExceptionCode errCode) {
        if (!express) {
            throw new CommonException(errCode.getCode());
        }
    }

    public static final void isTrue(boolean express, ExceptionCode errCode, String message) {
        if (!express) {
            throw new CommonException(errCode.getCode(), message);
        }
    }

    public static final void notNull(Object object, ExceptionCode errCode) {
        if (object == null) {
            throw new CommonException(errCode.getCode());
        }
    }

    public static final void notNull(Object object, ExceptionCode errCode, String message) {
        if (object == null) {
            throw new CommonException(errCode.getCode(), message);
        }
    }

    public static final <T> void notEmpty(T[] array, ExceptionCode errCode) {
        if (array.length == 0) {
            throw new CommonException(errCode.getCode());
        }
    }

    public static final <T> void notEmpty(T[] array, ExceptionCode errCode, String message) {
        if (array.length == 0) {
            throw new CommonException(errCode.getCode(), message);
        }
    }

    public static final <T extends Collection<?>> void notEmpty(T collection, ExceptionCode errCode) {
        notNull(collection, errCode);
        if (collection.size() == 0) {
            throw new CommonException(errCode.getCode());
        }
    }

    public static final <T extends Collection<?>> void notEmpty(T collection, ExceptionCode errCode, String message) {
        notNull(collection, errCode, message);
        if (collection.size() == 0) {
            throw new CommonException(errCode.getCode(), message);
        }
    }
}

